﻿using System;
using Excel;

using BEGDC.Office.Interface;

namespace BEGDC.Office.Library
{
	/// <summary>
	/// Summary description for ReportExcel.
	/// </summary>
    public class ExcelSession : OfficeSession, IExcelSession
	{
        public ExcelSession()
		{
			
		}

		private ApplicationClass m_objApp;
		private Workbook m_objBook;
		private Worksheet m_objWorkSheet;

		public override void Close()
		{
            CloseApp();
			GC.Collect();
			GC.Collect();
            base.Close();
		}

        private void CloseApp()
        {
            if (this.m_objBook != null)
            {
                object objMissing = System.Reflection.Missing.Value;
                try
                {
                    this.m_objBook.Close(false, objMissing, objMissing);
                }
                finally
                {
                    try
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(this.m_objBook);                        
                    }
                    catch
                    {
                    }
                    this.m_objBook = null;
                }
            }
            if (this.m_objApp != null)
            {
                try
                {
                    this.m_objApp.Workbooks.Close();
                    this.m_objApp.Quit();
                }
                finally
                {
                    try
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(this.m_objApp);
                    }
                    catch
                    {
                    }
                    this.m_objApp = null;
                }
            }
        }

        public virtual string GetValue(string strPosition)
		{
            Range objRange = null;
            try
            {
                objRange = this.m_objWorkSheet.get_Range(strPosition, System.Reflection.Missing.Value);
                if (objRange.get_Value(System.Reflection.Missing.Value) != null)
                    return objRange.get_Value(System.Reflection.Missing.Value).ToString();
                else
                    return string.Empty;
            }
            finally
            {
                if (objRange != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);
                    objRange = null;
                }
            }
		}

        public override void Open(string fileName)
		{
			this.m_objApp = new Excel.ApplicationClass();
			object objMissing = System.Reflection.Missing.Value;
            this.m_objBook = this.m_objApp.Workbooks.Open(fileName, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
			this.m_objWorkSheet = (Worksheet)this.m_objBook.Worksheets[1];
            base.Open(fileName);
		}

        public void SelectSheet(string sheetName)
        {
            this.m_objWorkSheet = (Worksheet)this.m_objBook.Worksheets[sheetName];
        }

        public virtual void SetFormula(string strPostion, string strFormula)
		{
			Range objRange = this.m_objWorkSheet.get_Range(strPostion, System.Reflection.Missing.Value);
			objRange.Formula = strFormula;
		}

        public virtual void SetValue(string strPostion, string strValue)
		{
			Range objRange = this.m_objWorkSheet.get_Range(strPostion, System.Reflection.Missing.Value);
			objRange.set_Value(System.Reflection.Missing.Value, strValue);
		}

        public virtual void SetColor(string strPostion, int iColor)
		{
			Range objRange = this.m_objWorkSheet.get_Range(strPostion, System.Reflection.Missing.Value);
			objRange.Interior.ColorIndex = iColor;
		}

        public override void PrintPreview()
		{
			object objMissing = System.Reflection.Missing.Value;
			this.m_objWorkSheet.PrintPreview(objMissing);
		}

        public override void Print()
		{
			object objMissing = System.Reflection.Missing.Value;
			this.m_objWorkSheet.PrintOut(objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);		
		}

        public virtual void Unprotect(string strPassword)
		{
			object objMissing = System.Reflection.Missing.Value;				
			this.m_objWorkSheet.Unprotect(strPassword);
		}

        public virtual void Protect(string strPassword)
		{
			object objMissing = System.Reflection.Missing.Value;				
			this.m_objWorkSheet.Protect(strPassword,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing);
		}

        public override void SaveToWeb(string strFileName)
		{
			if (System.IO.File.Exists(strFileName))
				System.IO.File.Delete(strFileName);
			object objMissing = System.Reflection.Missing.Value;
			string strTemp = this.m_objBook.FullName;
			this.m_objWorkSheet.SaveAs(strFileName, Excel.XlFileFormat.xlHtml,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing);
			if (System.IO.File.Exists(strTemp))
				System.IO.File.Delete(strTemp);
			this.m_objWorkSheet.SaveAs(strTemp, objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing,objMissing);
		}

        public override void Save()
        {
            this.m_objApp.DisplayAlerts = false;
            object objMissing = System.Reflection.Missing.Value;
            string strTemp = this.m_objBook.FullName;

            this.m_objBook.Save();
            //this.m_objApp.Save(strTemp);
            this.m_objApp.DisplayAlerts = true;
        }

        public virtual bool Visible
		{
			get
			{
				return this.m_objApp.Visible;
			}
			set
			{
				this.m_objApp.Visible = value;
			}
		}
	}
}
